//
//  XSPlayVideoViewController.m
//  小说
//
//  Created by WZH on 2018/5/28.
//  Copyright © 2018年 WZH. All rights reserved.
//

#import "XSPlayVideoViewController.h"
@import MediaPlayer;
@interface XSPlayVideoViewController ()

@property (nonatomic, strong) UIButton *openWeb;

@property (nonatomic, strong) UIButton *openVideo;

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation XSPlayVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self configUI];
}

#pragma mark 初始化UI控件
- (void)initUI{
    self.view.backgroundColor = [UIColor whiteColor];
    self.openWeb = [UIButton new];
    [self.openWeb setTitle:@"用网页打开" forState:UIControlStateNormal];
    self.openWeb.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.openWeb];
    self.openVideo = [UIButton new];
    [self.openVideo setTitle:@"打开视频" forState:UIControlStateNormal];
    self.openVideo.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.openVideo];
    [self.openWeb addTarget:self action:@selector(openWebClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.openVideo addTarget:self action:@selector(openVideoClick:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    [rightButton setTitle:@"Hidden" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    [rightButton addTarget:self action:@selector(rightClick:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark 为UI控件添加约束
- (void)configUI{
    [self.openWeb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(30);
        make.height.mas_offset(50);
        make.center.mas_offset(0);
    }];
    [self.openVideo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.openWeb);
        make.width.height.equalTo(self.openWeb);
        make.top.equalTo(self.openWeb.mas_bottom).mas_offset(30);
    }];
}

- (void)openWebClick:(UIButton *)sender{
    self.webView.hidden = NO;
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.model.htmlUrl]]];
}

- (void)openVideoClick:(UIButton *)sender{
    MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:self.model.url]];
    [self presentMoviePlayerViewControllerAnimated:vc];
//    [self presentViewController:vc animated:YES completion:^{
    
//    }];
}

#pragma mark 导航右侧按钮点击事件
- (void)rightClick:(UIButton *)sender{
    self.webView.hidden = !self.webView.isHidden;
}

- (UIWebView *)webView{
    if (!_webView) {
        _webView = [UIWebView new];
        [self.view addSubview:_webView];
        [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_offset(0);
            make.top.equalTo(self.mas_topLayoutGuide);
        }];
        _webView.hidden = YES;
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.model.htmlUrl]]];
    }
    return _webView;
}

@end
